'use strict';

const moment = require('moment');

const Controller = require('egg').Controller;

// const xlsxFun = require('./xlsx');

class CommonController extends Controller {

  success(data = {}, errmsg = 'request success', errno = 0) {
    const { from = 1, size = 10, isDownload = false, } = this.ctx.request.query;

    const payload = { errno, errmsg, updateAt: moment().format('YYYY-MM-DD HH:mm'), };

    if (data && data.rows) {
      Object.assign(payload, {
        pagination: { from: Number(from), size: Number(size), count: data.count, },
        data: data.rows,
      });
    } else if (data && data.extra) {
      Object.assign(payload, {
        extra: data.extra,
        data,
      });
    } else {
      Object.assign(payload, {
        data,
      });
    }

    if (isDownload) {
      // this.ctx.body = payload;
      // this.ctx.body = this.ctx.helper.normalizeXlsx('data');
    } else {
      this.ctx.body = payload;
    }
  }

}
module.exports = CommonController;